package com.whale.cloud.util.rest.schema;

import com.whale.cloud.exception.CommonException;
import com.whale.cloud.util.rest.condition.EVoluationType;
import com.whale.cloud.util.rest.schema.T_SYS_COLUMN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/** Created by paco on 6/6/2017. */
@Component
@Scope("prototype")
public class ColumnImpl implements Column {
  @Autowired private FunctionManager tableManager;

  private Function table;
  private T_SYS_COLUMN column;

  public void setFunction(Function t) {
    if (t == null) {
      throw new CommonException("-50", "初始字段时，表信息不能为空");
    }
    this.table = t;
  }

  public void setColumn(T_SYS_COLUMN c) {
    if (c == null) {
      throw new CommonException("-50", "初始字段时，字段信息不能为空");
    }
    this.column = c;
  }

  @Override
  public long getId() {
    return column.getId();
  }

  @Override
  public String getDb_name() {
    return column.getField_name();
  }

  @Override
  public String getShow_name() {
    return column.getShow_name();
  }

  @Override
  public Function getForeign_table() {
    return tableManager.getById(column.getForeign_table_id());
  }

  @Override
  public boolean isPk() {
    return "Y".equalsIgnoreCase(column.getIs_pk());
  }

  @Override
  public EVoluationType getVoluationType() {
    return EVoluationType.getEVoluationType(column.getVoluation_type());
  }

  @Override
  public boolean isNullable() {
    return "Y".equalsIgnoreCase(column.getIs_nullable());
  }

  @Override
  public String getDefaultValue() {
    return column.getDefault_value();
  }
}
